﻿package ua.mihaylenko.cardsplugin.components 
{
	import flash.display.SimpleButton;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.net.navigateToURL;
	import flash.system.System;
	import ua.mihaylenko.cardsplugin.common.DocumentClass;
	import ua.mihaylenko.cardsplugin.common.FlashVars;
	import ua.mihaylenko.cardsplugin.cardsviwer.Card;
	
	public class EmbedPanel extends Sprite
	{
		public var _embedTxt:TextField;
		public var _embedDesignTxt:TextField;
		public var _embedCommentTxt:TextField;
		public var copyBtn:SimpleButton;
		public var orderPrintBtn:SimpleButton;
		public var designerNameBtn;
		public var _embedDesignerTxt:TextField;
		public var _linkBtn:Sprite;
		private var sUrl:String;
		private var _smcCard:Card;
		
		public function EmbedPanel() 
		{
			_linkBtn.buttonMode = true;
			//copyBtn.buttonMode = true;
			
			orderPrintBtn.visible=false;
			_embedDesignerTxt.visible=false;
			designerNameBtn.visible=false;
			designerNameBtn.mouseChildren=false;
			designerNameBtn.buttonMode=true;
			designerNameBtn.designerName.autoSize=TextFieldAutoSize.LEFT;
			//_embedCommentTxt.autoSize=TextFieldAutoSize.LEFT;
			_embedCommentTxt.text="";
			
			sUrl = DocumentClass.AppStage.loaderInfo.parameters["params"];
			//FlashVars.getInstance().getData("params").toString();
			//DocumentClass.AppStage.loaderInfo.parameters["params"];						
			if (!sUrl) 
			{
				sUrl = "http://realestate.zetaprints.com/RssTemplates.aspx?2D245F1D-BAFE-47B5-93E5-63805754A54B";
			}
			_embedTxt.text = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" width=\""+DocumentClass.AppStage.stageWidth+"\" height=\""+DocumentClass.AppStage.stageHeight+"\" id=\"e-cards-plugin\" align=\"middle\">"+
							 "<param name=\"allowScriptAccess\" value=\"sameDomain\" />"+
							 "<param name=\"allowFullScreen\" value=\"false\" />"+
							 "<param name=\"movie\" value=\""+DocumentClass.Url+"\" />"+
							 "<param name=\"quality\" value=\"high\" />"+
							 "<param name=\"bgcolor\" value=\"#666666\" />"+
							 "<param name=\"FlashVars\" vaule=\"params="+sUrl+"\" />"+
							 "<embed src =\"" + DocumentClass.Url + "\" quality =\"high\" bgcolor =\"#666666\" width =\"" + DocumentClass.AppStage.stageWidth + "\" height =\"" + DocumentClass.AppStage.stageHeight + "\" name =\"e-cards-plugin\" align =\"middle\" allowScriptAccess =\"sameDomain\" allowFullScreen =\"false\" type =\"application/x-shockwave-flash\" pluginspage =\"http://www.macromedia.com/go/getflashplayer\" FlashVars =\"params=" + sUrl + "\">" +
							 "</embed>"+
							 "</object>";
	
			_embedTxt.borderColor = 0xD1D1D3;
			_embedTxt.addEventListener(MouseEvent.CLICK, embedTextClickHandler);
			//_embedDesignTxt.htmlText = "Embed plugin with your own designs: <a href=\"http://www.zetaprints.com/\"> <b><u>Find out how</u></b></a>";
			copyBtn.addEventListener(MouseEvent.CLICK, copyClickHandler);
			_linkBtn.addEventListener(MouseEvent.CLICK, linkHandler);
			orderPrintBtn.addEventListener(MouseEvent.CLICK,orderPrintClickHandler);
			designerNameBtn.addEventListener(MouseEvent.CLICK,designerClickHandler);
			
		}
		
		private function embedTextClickHandler(event:MouseEvent):void
		{
			_embedTxt.text = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" width=\""+DocumentClass.AppStage.stageWidth+"\" height=\""+DocumentClass.AppStage.stageHeight+"\" id=\"e-cards-plugin\" align=\"middle\">"+
							 "<param name=\"allowScriptAccess\" value=\"sameDomain\" />"+
							 "<param name=\"allowFullScreen\" value=\"false\" />"+
							 "<param name=\"movie\" value=\""+DocumentClass.Url+"\" />"+
							 "<param name=\"quality\" value=\"high\" />"+
							 "<param name=\"bgcolor\" value=\"#666666\" />"+
							 "<param name=\"FlashVars\" vaule=\"params="+sUrl+"\" />"+
							 "<embed src =\"" + DocumentClass.Url + "\" quality =\"high\" bgcolor =\"#666666\" width =\"" + DocumentClass.AppStage.stageWidth + "\" height =\"" + DocumentClass.AppStage.stageHeight + "\" name =\"e-cards-plugin\" align =\"middle\" allowScriptAccess =\"sameDomain\" allowFullScreen =\"false\" type =\"application/x-shockwave-flash\" pluginspage =\"http://www.macromedia.com/go/getflashplayer\" FlashVars =\"params=" + sUrl + "\">" +
							 "</embed>"+
							 "</object>";
			_embedTxt.setSelection(0, _embedTxt.length);
		}
		
		private function copyClickHandler(event:MouseEvent):void
		{
			_embedTxt.text = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" width=\""+DocumentClass.AppStage.stageWidth+"\" height=\""+DocumentClass.AppStage.stageHeight+"\" id=\"e-cards-plugin\" align=\"middle\">"+
							 "<param name=\"allowScriptAccess\" value=\"sameDomain\" />"+
							 "<param name=\"allowFullScreen\" value=\"false\" />"+
							 "<param name=\"movie\" value=\""+DocumentClass.Url+"\" />"+
							 "<param name=\"quality\" value=\"high\" />"+
							 "<param name=\"bgcolor\" value=\"#666666\" />"+
							 "<param name=\"FlashVars\" vaule=\"params="+sUrl+"\" />"+
							 "<embed src =\"" + DocumentClass.Url + "\" quality =\"high\" bgcolor =\"#666666\" width =\"" + DocumentClass.AppStage.stageWidth + "\" height =\"" + DocumentClass.AppStage.stageHeight + "\" name =\"e-cards-plugin\" align =\"middle\" allowScriptAccess =\"sameDomain\" allowFullScreen =\"false\" type =\"application/x-shockwave-flash\" pluginspage =\"http://www.macromedia.com/go/getflashplayer\" FlashVars =\"params=" + sUrl + "\">" +
							 "</embed>"+
							 "</object>";
			System.setClipboard(_embedTxt.text);
		}
		
		private function linkHandler(event:MouseEvent):void
		{
			navigateToURL(new URLRequest("http://www.zetaprints.com/help/tools/image-generation-plugin/"), "_blank");
		}
		
		public function updateData(card=null){
			//if there is no designer details
			if(card==null){
				orderPrintBtn.visible=false;
			   	_embedDesignerTxt.visible=false;
				designerNameBtn.visible=false;
				_embedCommentTxt.text="";
				return;
			}
			_smcCard=card;
			
			//if the download attribute is not set to "only"
			//display [order prints] button
			//else if designer details are available, 
			//display text [Designed by : designer name] linked to his profile page
			_embedCommentTxt.text=_smcCard._sComments;
			_embedCommentTxt.x=copyBtn.x+copyBtn.width+10;
			_embedCommentTxt.width=stage.stageWidth-(_embedCommentTxt.x+15);
			
			if(_smcCard._sDownload!="only"){
			   orderPrintBtn.visible=true;
			   orderPrintBtn.x=stage.stageWidth-(orderPrintBtn.width+15);
			   designerNameBtn.visible=false;
			}else{
				if(_smcCard._sDesignerName){					
					designerNameBtn.designerName.htmlText="<b>"+_smcCard._sDesignerName+"</b>";
					designerNameBtn.designerBtnBG.width=designerNameBtn.designerName.width+3;
					designerNameBtn.visible=true;
					designerNameBtn.x=stage.stageWidth-(designerNameBtn.width+15);
					_embedDesignerTxt.visible=true;
					_embedDesignerTxt.x=designerNameBtn.x-(_embedDesignerTxt.width);
					orderPrintBtn.visible=false;
					
				}
				return;
			}
			/*trace(_smcCard._sDesignerProfileUrl);
			trace(_smcCard._sDesignerID);
			trace(_smcCard._sDesignerName);
			trace(_smcCard._sDownload);*/
		}
		
		private function orderPrintClickHandler(event:MouseEvent):void{
			var sURL:String = new String(FlashVars.getInstance().getData("domain") + "/?page=template;TemplateID=" +_smcCard.id);
			navigateToURL(new URLRequest(sURL),"_blank");
		}
		
		private function designerClickHandler(event:MouseEvent):void{
			navigateToURL(new URLRequest(_smcCard._sDesignerProfileUrl),"_blank");
		}
	}	
}